﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data.MySqlClient;


//------------------------------------------------------
//クラス：H3A1 07
//名　前：仮屋　智央
//内　容：出欠を取る科目と時限を選択する画面
//　　　：
//------------------------------------------------------

namespace h3a1_felica2
{
    public partial class Form2 : Form
    {
        Timer timer = new Timer();
        public Form2()
        {
            InitializeComponent();

            //フォームサイズを固定
            this.FormBorderStyle = FormBorderStyle.FixedToolWindow;
            this.Text = "授業選択";
            this.label1.Text = Parameter.lecturer_name + "さん"; //ログイン者の名前を取得
            this.label2.Text = "科目名";
            this.label3.Text = "時限目";
            this.button1.Text = "ＯＫ";

            this.FormClosing += new FormClosingEventHandler(Form2_FormClosing);
        }

        private void Form2_Load(object sender, EventArgs e)
        {

            string uri = System.Configuration.ConfigurationSettings.AppSettings["uri"];
            string database = System.Configuration.ConfigurationSettings.AppSettings["database"];
            try
            {
                MySqlConnection cn;
                MySqlCommand cmd;
                MySqlDataReader reader;
                cn = new MySqlConnection();
                cn.ConnectionString = "Server=" + uri + ";Uid=root;Pwd=mysql;database=" + database + ";";
                cmd = new MySqlCommand();
                cmd.Connection = cn;
                cn.Open();
                cmd.CommandText = "select class_subject_name from class_subject;";
                reader = cmd.ExecuteReader();

                //対象科目をアイテムに追加
                while (reader.Read())
                {
                    this.comboBox1.Items.Add(reader[0]);
                }
                reader.Close();

                //時限をアイテムに追加
                cmd.CommandText = "select hour_name from hour;";
                reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    this.comboBox2.Items.Add(reader[0]);
                }

                reader.Close();
                cn.Close();
            }
            catch (MySqlException e1)
            {
                MessageBox.Show(e1.Message);
            }

        }

        //ＯＫボタンを押した時の処理
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.comboBox1.SelectedItem.ToString() != null && this.comboBox2.SelectedItem.ToString() != null)
                {
                    //座標の取得
                    int map_x = this.Location.X;
                    int map_y = this.Location.Y;

                    Parameter.class_subject_name = comboBox1.SelectedItem.ToString();                    
                    Parameter.hour_id = int.Parse(comboBox2.SelectedItem.ToString().Substring(0, 1));

                    Form3 form3 = new Form3(this.comboBox1.SelectedItem.ToString(), this.comboBox2.SelectedItem.ToString(), this);
                    form3.StartPosition = FormStartPosition.Manual;
                    form3.Location = new Point(map_x, map_y);
                    this.Hide();
                    form3.ShowDialog();
                }
            }
            catch (NullReferenceException e1)
            {
                MessageBox.Show("科目・時限目の両方を選択してください");
            }
        }

        // FormClosing イベントのイベントハンドラ
        private void Form2_FormClosing(object sender, FormClosingEventArgs e)
        {
            Application.Exit();
        }

        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
    }
}

